package com.wuliqing.android.zte.fragment;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wuliqing.android.zte.data.ContactData;
import com.wuliqing.android.zte.myapplication.DetailActivity;
import com.wuliqing.android.zte.myapplication.R;
import com.wuliqing.android.zte.presenter.LoadContactListPresenter;
import com.wuliqing.android.zte.service.ContactSaveService;
import com.wuliqing.android.zte.view.RecyclerViewsAdapter;

public class ListFragment extends Fragment implements RecyclerViewsAdapter.OnRecyclerViewListener,
        LoadContactListPresenter.LoadContactDataListener {

    private RecyclerView mRecyclerView;
    private LoadContactListPresenter mLoadContactListPresenter;
    private RecyclerViewsAdapter mAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLoadContactListPresenter = new LoadContactListPresenter(getActivity(), this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRecyclerView =
                (RecyclerView) inflater.inflate(R.layout.list_fragment, container, false);
        mRecyclerView.setHasFixedSize(true);
        return mRecyclerView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        mRecyclerView.setLayoutManager(new LinearLayoutManager(mRecyclerView.getContext()));
//        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(),4));
        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        mAdapter = new RecyclerViewsAdapter(getActivity());
        mAdapter.setOnRecyclerViewListener(this);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//        mRecyclerView.addItemDecoration(new DividerItemDecoration(
//                getActivity(), DividerItemDecoration.HORIZONTAL_LIST));
        mLoadContactListPresenter.onStart();
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mLoadContactListPresenter.onDestroy();
    }

    @Override
    public void onItemClick(final ContactData mContactData) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(mRecyclerView, "translationZ", 20, 0);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                //mContactData
                Intent intent = new Intent(getActivity(), DetailActivity.class);
                //intent.putExtra(ContactData.CONTACT_DATA_TAG, mContactData.get(position));
                intent.setData(getContactUri(mContactData));
                startActivity(intent);
            }
        });
        animator.start();
    }

    @Override
    public boolean onItemLongClick(final ContactData mContactData) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        //View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_view, null);
        //builder.setView(view);
//        builder.setTitle("hello");
        builder.setMessage(getString(R.string.delete_contact_msg));
        builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = ContactSaveService.createDeleteContactIntent(getActivity(), getContactUri(mContactData), null, null);
                getActivity().startService(intent);
            }
        });
        builder.create().show();
        return true;
    }

    public Uri getContactUri(ContactData contactData) {
        Uri uri = null;
        if (contactData != null && contactData.getContact_id() != 0 && contactData.getLookup_key() != null) {
            uri = ContactsContract.Contacts.getLookupUri(contactData.getContact_id(), contactData.getLookup_key());
        }
        return uri;
    }

    @Override
    public void onCallButtonClick(ContactData mContactData) {
//        if (mContactData.size() > position) {
//            ContactData contactData = mContactData.get(position);
//            if (contactData.getContact_number() != null) {
//                Intent intent = CallUtil.getCallIntent(contactData.getContact_number());
//                startActivity(intent);
//            }
//        }
    }

    @Override
    public void onSelectImageClick(ContactData mContactData) {
//        Toast.makeText(getActivity(),"position = " + position,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void requestContactData(Cursor cursor) {
//        mContactData = list;
//        mRecyclerViewAdapter.notifyDataSetChanged();
        mAdapter.changeCursor(cursor);
    }
}
